<template>
  <div class="login_container">
    <div class="login_box">
      <div class="login_img">
        <img src="../assets/logo_mobile.png" alt="" />
      </div>
      <el-form
        label-width="50px"
        class="login_form"
        :model="loginForm"
        :rules="loginFormRules"
        :hide-required-asterisk="true"
        ref="loginFormRef"
      >
        <el-form-item label="账号" prop="username">
          <el-input
            suffix-icon="el-icon-user-solid"
            v-model="loginForm.username"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            suffix-icon="el-icon-key"
            v-model="loginForm.password"
            type="password"
          ></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" plain @click="login">登录</el-button>
          <el-button type="info" plain @click="loginFormReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      //登陆表单数据绑定
      loginForm: {
        username: "admin",
        password: "123456",
      },
      loginFormRules: {
        username: [
          { required: true, message: "请输入账户名称" },
          { min: 4, max: 20, message: "长度在 4 到 20 个字符" },
        ],
        password: [{ min: 5, max: 15, message: "长度在 5 到 15 个字符" }],
      },
    };
  },
  methods: {
    loginFormReset() {
      this.$refs.loginFormRef.resetFields();
    },
    login() {
      this.$refs.loginFormRef.validate((valid) => {
        if (!valid) return;
        this.$axios
          .get("/users/login", {
            params: {
              username: this.loginForm.username,
              password: this.loginForm.password,
            },
          })
          .then((res) => {
            console.log(res);
            if (res.data.code === 400) {
              this.$message.error(res.data.res);
            } else {
              this.$message.success("登录成功");
              window.sessionStorage.setItem("token", res.data.res.token);
              window.sessionStorage.setItem("users", res.data.res.users);
              this.$router.push({ path: "/home" });
              //console.log(sessionStorage)
            }
          })
          .catch();
      });
    },
  },
};
</script>

<style lang="less" scoped>
.login_container {
  background-image: linear-gradient(to bottom, white 60%, #7a9999);
  height: 100%;
  position: relative;
}
.login_box {
  background-image: linear-gradient(to bottom, white 30%);
  height: 300px;
  width: 450px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  border-radius: 10px;
}
.login_img {
  height: 100px;
}
.login_img > img {
  background-color: white;
  position: relative;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
.btns {
  display: flex;
  justify-content: flex-end;
}
.login_box {
  border: solid 1px #7a9999;
  box-shadow: 0px 0px 10px #7a9999;
}
.login_form {
  position: absolute;
  bottom: 10px;
  width: 100%;
  padding: 0 30px;
  box-sizing: border-box;
  text-shadow: 1px 1px 2px #7a9999;
}
</style>